home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / implicit.c < prev    next >
C/C++ Source or Header  |  1993-03-10  |  18KB  |  580 lines

  1. /* Implicit rule searching for GNU Make.
  2. Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "rule.h"
  21. #include "dep.h"
  22. #include "file.h"
  23.  
  24. static int pattern_search ();
  25.  
  26. /* For a FILE which has no commands specified, try to figure out some
  27.    from the implicit pattern rules.
  28.    Returns 1 if a suitable implicit rule was found,
  29.    after modifying FILE to contain the appropriate commands and deps,
  30.    or returns 0 if no implicit rule was found.  */
  31.  
  32. int
  33. try_implicit_rule (file, depth)
  34.      struct file *file;
  35.      unsigned int depth;
  36. {
  37.   DEBUGPR ("Looking for an implicit rule for `%s'.\n");
  38.  
  39. #ifndef    NO_ARCHIVES
  40.   /* If this is an archive member reference, use just the
  41.      archive member name to search for implicit rules.  */
  42.   if (ar_name (file->name))
  43.     {
  44.       DEBUGPR ("Looking for archive-member implicit rule for `%s'.\n");
  45.       if (pattern_search (file, 1, depth, 0))
  46.     return 1;
  47.     }
  48. #endif
  49.  
  50.   return pattern_search (file, 0, depth, 0);
  51. }
  52.  
  53. #define DEBUGP2(msg, a1, a2)                              \
  54.   do {                                          \
  55.     if (debug_flag)                                  \
  56.       { print_spaces (depth); printf (msg, a1, a2); fflush (stdout); }          \
  57.   } while (0)
  58.  
  59. /* Search the pattern rules for a rule with an existing dependency to make
  60.    FILE.  If a rule is found, the appropriate commands and deps are put in FILE
  61.    and 1 is returned.  If not, 0 is returned.
  62.  
  63.    If ARCHIVE is nonzero, FILE->name is of the form "LIB(MEMBER)".  A rule for
  64.    "(MEMBER)" will be searched for, and "(MEMBER)" will not be chopped up into
  65.    directory and filename parts.
  66.  
  67.    If an intermediate file is found by pattern search, the intermediate file
  68.    is set up as a target by the recursive call and is also made a dependency
  69.    of FILE.
  70.  
  71.    DEPTH is used for debugging messages.  */
  72.  
  73. static int
  74. pattern_search (file, archive, depth, recursions)
  75.      struct file *file;
  76.      int archive;
  77.      unsigned int depth;
  78.      unsigned int recursions;
  79. {
  80.   /* Filename we are searching for a rule for.  */
  81.   char *filename = archive ? index (file->name, '(') : file->name;
  82.  
  83.   /* Length of FILENAME.  */
  84.   unsigned int namelen = strlen (filename);
  85.  
  86.   /* The last slash in FILENAME (or nil if there is none).  */
  87.   char *lastslash;
  88.  
  89.   /* This is a file-object used as an argument in
  90.      recursive calls.  It never contains any data
  91.      except during a recursive call.  */
  92.   struct file *intermediate_file = 0;
  93.  
  94.   /* List of dependencies found recursively.  */
  95.   struct file **intermediate_files
  96.     = (struct file **) alloca (max_pattern_deps * sizeof (struct file *));
  97.  
  98.   /* List of the patterns used to find intermediate files.  */
  99.   char **intermediate_patterns
  100.     = (char **) alloca (max_pattern_deps * sizeof (char *));
  101.  
  102.   /* This buffer records all the dependencies actually found for a rule.  */
  103.   char **found_files = (char **) alloca (max_pattern_deps * sizeof (char *));
  104.   /* Number of dep names now in FOUND_FILES.  */
  105.   unsigned int deps_found;
  106.  
  107.   /* Names of possible dependencies are constructed in this buffer.  */
  108.   register char *depname = (char *) alloca (namelen + max_pattern_dep_length);
  109.  
  110.   /* The start and length of the stem of FILENAME for the current rule.  */
  111.   register char *stem;
  112.   register unsigned int stemlen;
  113.  
  114.   /* Buffer in which we store all the rules that are possibly applicable.  */
  115.   struct rule **tryrules
  116.     = (struct rule **) alloca (num_pattern_rules * sizeof (struct rule *));
  117.  
  118.   /* Number of valid elements in TRYRULES.  */
  119.   unsigned int nrules;
  120.  
  121.   /* The numbers of the rule targets of each rule
  122.      in TRYRULES that matched the target file.  */
  123.   unsigned int *matches
  124.     = (unsigned int *) alloca (num_pattern_rules * sizeof (unsigned int));
  125.  
  126.   /* Each element is nonzero if LASTSLASH was used in
  127.      matching the corresponding element of TRYRULES.  */
  128.   char *checked_lastslash
  129.     = (char *) alloca (num_pattern_rules * sizeof (char));
  130.  
  131.   /* The index in TRYRULES of the rule we found.  */
  132.   unsigned int foundrule;
  133.  
  134.   /* Nonzero if should consider intermediate files as dependencies.  */
  135.   int intermed_ok;
  136.  
  137.   /* Nonzero if we have matched a pattern-rule target
  138.      that is not just `%'.  */
  139.   int specific_rule_matched = 0;
  140.  
  141.   register unsigned int i;
  142.   register struct rule *rule;
  143.   register struct dep *dep;
  144.  
  145.   char *p;
  146.  
  147. #ifndef    NO_ARCHIVES
  148.   if (archive || ar_name (filename))
  149.     lastslash = 0;
  150.   else
  151. #endif
  152.     {
  153.       /* Set LASTSLASH to point at the last slash in FILENAME
  154.      but not counting any slash at the end.  (foo/bar/ counts as
  155.      bar/ in directory foo/, not empty in directory foo/bar/.)  */
  156.       lastslash = rindex (filename, '/');
  157.       if (lastslash != 0 && lastslash[1] == '\0')
  158.     lastslash = 0;
  159.     }
  160.  
  161.   /* First see which pattern rules match this target
  162.      and may be considered.  Put them in TRYRULES.  */
  163.  
  164.   nrules = 0;
  165.   for (rule = pattern_rules; rule != 0; rule = rule->next)
  166.     {
  167.       int specific_rule_may_have_matched = 0;
  168.       int check_lastslash;
  169.  
  170.       /* If the pattern rule has deps but no commands, ignore it.
  171.      Users cancel built-in rules by redefining them without commands.  */
  172.       if (rule->deps != 0 && rule->cmds == 0)
  173.     continue;
  174.  
  175.       /* If this rule is in use by a parent pattern_search,
  176.      don't use it here.  */
  177.       if (rule->in_use)
  178.     {
  179.       DEBUGP2 ("Avoiding implicit rule recursion.%s%s\n", "", "");
  180.       continue;
  181.     }
  182.  
  183.       for (i = 0; rule->targets[i] != 0; ++i)
  184.     {
  185.       char *target = rule->targets[i];
  186.       char *suffix = rule->suffixes[i];
  187.  
  188.       /* Rules that can match any filename and are not terminal
  189.          are ignored if we're recursing, so that they cannot be
  190.          intermediate files.  */
  191.       if (recursions > 0 && target[1] == '\0' && !rule->terminal)
  192.         continue;
  193.  
  194.       if (rule->lens[i] > namelen)
  195.         /* It can't possibly match.  */
  196.         continue;
  197.  
  198.       /* From the lengths of the filename and the pattern parts,
  199.          find the stem: the part of the filename that matches the %.  */
  200.       stem = filename + (suffix - target - 1);
  201.       stemlen = namelen - rule->lens[i] + 1;
  202.  
  203.       /* Set CHECK_LASTSLASH if FILENAME contains a directory
  204.          prefix and the target pattern does not contain a slash.  */
  205.  
  206.       check_lastslash = lastslash != 0 && index (target, '/') == 0;
  207.       if (check_lastslash)
  208.         {
  209.           /* In that case, don't include the
  210.          directory prefix in STEM here.  */
  211.           unsigned int difference = lastslash - filename + 1;
  212.           if (difference > stemlen)
  213.         continue;
  214.           stemlen -= difference;
  215.           stem += difference;
  216.         }
  217.  
  218.       /* Check that the rule pattern matches the text before the stem.  */
  219.       if (check_lastslash)
  220.         {
  221.           if (stem > (lastslash + 1)
  222.           && strncmp (target, lastslash + 1, stem - lastslash - 1))
  223.         continue;
  224.         }
  225.       else if (stem > filename
  226.            && strncmp (target, filename, stem - filename))
  227.         continue;
  228.  
  229.       /* Check that the rule pattern matches the text after the stem.
  230.          We could test simply use streq, but this way we compare the
  231.          first two characters immediately.  This saves time in the very
  232.          common case where the first character matches because it is a
  233.          period.  */
  234.       if (*suffix != stem[stemlen]
  235.           || (*suffix != '\0' && !streq (&suffix[1], &stem[stemlen + 1])))
  236.         continue;
  237.  
  238.       /* Record if we match a rule that not all filenames will match.  */
  239.       if (target[1] != '\0')
  240.         specific_rule_may_have_matched = 1;
  241.  
  242.       /* We have a matching target.  Don't search for any more.  */
  243.       break;
  244.     }
  245.  
  246.       /* None of the targets matched.  */
  247.       if (rule->targets[i] == 0)
  248.     continue;
  249.  
  250.       specific_rule_matched |= specific_rule_may_have_matched;
  251.  
  252.       /* A rule with no dependencies and no commands exists solely to set
  253.      specific_rule_matched when it matches.  Don't try to use it.  */
  254.       if (rule->deps == 0 && rule->cmds == 0)
  255.     continue;
  256.  
  257.       /* Record this rule in TRYRULES and the index
  258.      of the (first) matching target in MATCHES.  */
  259.       tryrules[nrules] = rule;
  260.       matches[nrules] = i;
  261.       checked_lastslash[nrules] = check_lastslash;
  262.       ++nrules;
  263.     }
  264.  
  265.   /* If we have found a matching rule that won't match all filenames,
  266.      retroactively reject any "terminal" rules that do always match.  */
  267.   if (specific_rule_matched)
  268.     for (i = 0; i < nrules; ++i)
  269.       if (!tryrules[i]->terminal)
  270.     {
  271.       register unsigned int j;
  272.       for (j = 0; tryrules[i]->targets[j] != 0; ++j)
  273.         if (tryrules[i]->targets[j][1] == '\0')
  274.           break;
  275.       if (tryrules[i]->targets[j] != 0)
  276.         tryrules[i] = 0;
  277.     }
  278.  
  279.   /* Try each rule once without intermediate files, then once with them.  */
  280.   for (intermed_ok = 0; intermed_ok == !!intermed_ok; ++intermed_ok)
  281.     {
  282.       /* Try each pattern rule till we find one that applies.
  283.      If it does, copy the names of its dependencies (as substituted)
  284.      and store them in FOUND_FILES.  DEPS_FOUND is the number of them.  */
  285.  
  286.       for (i = 0; i < nrules; i++)
  287.     {
  288.       int check_lastslash;
  289.  
  290.       rule = tryrules[i];
  291.  
  292.       /* RULE is nil when we discover that a rule,
  293.          already placed in TRYRULES, should not be applied.  */
  294.       if (rule == 0)
  295.         continue;
  296.  
  297.       /* Reject any terminal rules if we're
  298.          looking to make intermediate files.  */
  299.       if (intermed_ok && rule->terminal)
  300.         continue;
  301.  
  302.       /* Mark this rule as in use so a recursive
  303.          pattern_search won't try to use it.  */
  304.       rule->in_use = 1;
  305.  
  306.       /* From the lengths of the filename and the matching pattern parts,
  307.          find the stem: the part of the filename that matches the %.  */
  308.       stem = filename
  309.         + (rule->suffixes[matches[i]] - rule->targets[matches[i]]) - 1;
  310.       stemlen = namelen - rule->lens[matches[i]] + 1;
  311.       check_lastslash = checked_lastslash[i];
  312.       if (check_lastslash)
  313.         {
  314.           stem += lastslash - filename + 1;
  315.           stemlen -= (lastslash - filename) + 1;
  316.         }
  317.  
  318.       DEBUGP2 ("Trying pattern rule with stem `%.*s'.\n",
  319.            (int) stemlen, stem);
  320.  
  321.       /* Try each dependency; see if it "exists".  */
  322.  
  323.       deps_found = 0;
  324.       for (dep = rule->deps; dep != 0; dep = dep->next)
  325.         {
  326.           /* If the dependency name has a %, substitute the stem.  */
  327.           p = index (dep_name (dep), '%');
  328.           if (p != 0)
  329.         {
  330.           register unsigned int i;
  331.           if (check_lastslash)
  332.             {
  333.               /* Copy directory name from the original FILENAME.  */
  334.               i = lastslash - filename + 1;
  335.               bcopy (filename, depname, i);
  336.             }
  337.           else
  338.             i = 0;
  339.           bcopy (dep_name (dep), depname + i, p - dep_name (dep));
  340.           i += p - dep_name (dep);
  341.           bcopy (stem, depname + i, stemlen);
  342.           i += stemlen;
  343.           strcpy (depname + i, p + 1);
  344.           p = depname;
  345.         }
  346.           else
  347.         p = dep_name (dep);
  348.  
  349.           /* P is now the actual dependency name as substituted.  */
  350.  
  351.           if (file_impossible_p (p))
  352.         {
  353.           /* If this dependency has already been ruled
  354.              "impossible", then the rule fails and don't
  355.              bother trying it on the second pass either
  356.              since we know that will fail too.  */
  357.           DEBUGP2 ("Rejecting impossible %s dependency `%s'.\n",
  358.                p == depname ? "implicit" : "rule", p);
  359.           tryrules[i] = 0;
  360.           break;
  361.         }
  362.  
  363.           intermediate_files[deps_found] = 0;
  364.  
  365.           DEBUGP2 ("Trying %s dependency `%s'.\n",
  366.                p == depname ? "implicit" : "rule", p);
  367.  
  368.           /* The DEP->changed flag says that this dependency resides in a
  369.          nonexistent directory.  So we normally can skip looking for
  370.          the file.  However, if CHECK_LASTSLASH is set, then the
  371.          dependency file we are actually looking for is in a different
  372.          directory (the one gotten by prepending FILENAME's directory),
  373.          so it might actually exist.  */
  374.  
  375.           if ((!dep->changed || check_lastslash)
  376.           && (lookup_file (p) != 0 || file_exists_p (p)))
  377.         {
  378.           found_files[deps_found++] = savestring (p, strlen (p));
  379.           continue;
  380.         }
  381.           /* This code, given FILENAME = "lib/foo.o", dependency name
  382.          "lib/foo.c", and VPATH=src, searches for "src/lib/foo.c".  */
  383.           if (vpath_search (&p, (time_t *) 0))
  384.         {
  385.           DEBUGP2 ("Found dependency as `%s'.%s\n", p, "");
  386.           found_files[deps_found++] = p;
  387.           continue;
  388.         }
  389.  
  390.           /* We could not find the file in any place we should look.
  391.          Try to make this dependency as an intermediate file,
  392.          but only on the second pass.  */
  393.  
  394.           if (intermed_ok)
  395.         {
  396.           if (intermediate_file == 0)
  397.             intermediate_file
  398.               = (struct file *) alloca (sizeof (struct file));
  399.  
  400.           DEBUGP2 ("Looking for a rule with %s file `%s'.\n",
  401.                "intermediate", p);
  402.  
  403.           bzero ((char *) intermediate_file, sizeof (struct file));
  404.           intermediate_file->name = p;
  405.           if (pattern_search (intermediate_file, 0, depth + 1,
  406.                       recursions + 1))
  407.             {
  408.               p = savestring (p, strlen (p));
  409.               intermediate_patterns[deps_found]
  410.             = intermediate_file->name;
  411.               found_files[deps_found] = p;
  412.               intermediate_file->name = p;
  413.               intermediate_files[deps_found] = intermediate_file;
  414.               intermediate_file = 0;
  415.               ++deps_found;
  416.               continue;
  417.             }
  418.  
  419.           /* If we have tried to find P as an intermediate
  420.              file and failed, mark that name as impossible
  421.              so we won't go through the search again later.  */
  422.           file_impossible (p);
  423.         }
  424.  
  425.           /* A dependency of this rule does not exist.
  426.          Therefore, this rule fails.  */
  427.           break;
  428.         }
  429.  
  430.       /* This rule is no longer `in use' for recursive searches.  */
  431.       rule->in_use = 0;
  432.  
  433.       if (dep != 0)
  434.         {
  435.           /* This pattern rule does not apply.
  436.          If some of its dependencies succeeded,
  437.          free the data structure describing them.  */
  438.           while (deps_found-- > 0)
  439.         {
  440.           register struct file *f = intermediate_files[deps_found];
  441.           free (found_files[deps_found]);
  442.           if (f != 0
  443.               && (f->stem < f->name
  444.               || f->stem > f->name + strlen (f->name)))
  445.             free (f->stem);
  446.         }
  447.         }
  448.       else
  449.         /* This pattern rule does apply.  Stop looking for one.  */
  450.         break;
  451.     }
  452.  
  453.       /* If we found an applicable rule without
  454.      intermediate files, don't try with them.  */
  455.       if (i < nrules)
  456.     break;
  457.  
  458.       rule = 0;
  459.     }
  460.  
  461.   /* RULE is nil if the loop went all the way
  462.      through the list and everything failed.  */
  463.   if (rule == 0)
  464.     return 0;
  465.  
  466.   foundrule = i;
  467.  
  468.   /* If we are recursing, store the pattern that matched
  469.      FILENAME in FILE->name for use in upper levels.  */
  470.  
  471.   if (recursions > 0)
  472.     /* Kludge-o-matic */
  473.     file->name = rule->targets[matches[foundrule]];
  474.  
  475.   /* FOUND_FILES lists the dependencies for the rule we found.
  476.      This includes the intermediate files, if any.
  477.      Convert them into entries on the deps-chain of FILE.  */
  478.  
  479.   while (deps_found-- > 0)
  480.     {
  481.       register char *s;
  482.  
  483.       if (intermediate_files[deps_found] != 0)
  484.     {
  485.       /* If we need to use an intermediate file,
  486.          make sure it is entered as a target, with the info that was
  487.          found for it in the recursive pattern_search call.
  488.          We know that the intermediate file did not already exist as
  489.          a target; therefore we can assume that the deps and cmds
  490.          of F below are null before we change them.  */
  491.  
  492.       struct file *imf = intermediate_files[deps_found];
  493.       register struct file *f = enter_file (imf->name);
  494.       f->deps = imf->deps;
  495.       f->cmds = imf->cmds;
  496.       f->stem = imf->stem;
  497.       imf = lookup_file (intermediate_patterns[deps_found]);
  498.       if (imf != 0 && imf->precious)
  499.         f->precious = 1;
  500.       f->intermediate = 1;
  501.       f->tried_implicit = 1;
  502.       for (dep = f->deps; dep != 0; dep = dep->next)
  503.         {
  504.           dep->file = enter_file (dep->name);
  505.           dep->name = 0;
  506.           dep->file->tried_implicit |= dep->changed;
  507.         }
  508.       num_intermediates++;
  509.     }
  510.  
  511.       dep = (struct dep *) xmalloc (sizeof (struct dep));
  512.       s = found_files[deps_found];
  513.       if (recursions == 0)
  514.     {
  515.       dep->name = 0;
  516.       dep->file = enter_file (s);    
  517.     }
  518.       else
  519.     {
  520.       dep->name = s;
  521.       dep->file = 0;
  522.       dep->changed = 0;
  523.     }
  524.       if (intermediate_files[deps_found] == 0 && tryrules[foundrule]->terminal)
  525.     {
  526.       /* If the file actually existed (was not an intermediate file),
  527.          and the rule that found it was a terminal one, then we want
  528.          to mark the found file so that it will not have implicit rule
  529.          search done for it.  If we are not entering a `struct file' for
  530.          it now, we indicate this with the `changed' flag.  */
  531.       if (dep->file == 0)
  532.         dep->changed = 1;
  533.       else
  534.         dep->file->tried_implicit = 1;
  535.     }
  536.       dep->next = file->deps;
  537.       file->deps = dep;
  538.     }
  539.  
  540.   if (!checked_lastslash[foundrule])
  541.     file->stem = stem[stemlen] == '\0' ? stem : savestring (stem, stemlen);
  542.   else
  543.     {
  544.       /* We want to prepend the directory from
  545.      the original FILENAME onto the stem.  */
  546.       file->stem = (char *) xmalloc (((lastslash + 1) - filename)
  547.                      + stemlen + 1);
  548.       bcopy (filename, file->stem, (lastslash + 1) - filename);
  549.       bcopy (stem, file->stem + ((lastslash + 1) - filename), stemlen);
  550.       file->stem[((lastslash + 1) - filename) + stemlen] = '\0';
  551.     }
  552.  
  553.   file->cmds = rule->cmds;
  554.  
  555.   /* Put the targets other than the one that
  556.      matched into FILE's `also_make' member.  */
  557.  
  558.   /* If there was only one target, there is nothing to do.  */
  559.   if (rule->targets[1] != 0)
  560.     for (i = 0; rule->targets[i] != 0; ++i)
  561.       if (i != matches[foundrule])
  562.     {
  563.       struct dep *new = (struct dep *) xmalloc (sizeof (struct dep));
  564.       new->name = p = (char *) xmalloc (rule->lens[i] + stemlen + 1);
  565.       bcopy (rule->targets[i], p,
  566.          rule->suffixes[i] - rule->targets[i] - 1);
  567.       p += rule->suffixes[i] - rule->targets[i] - 1;
  568.       bcopy (stem, p, stemlen);
  569.       p += stemlen;
  570.       bcopy (rule->suffixes[i], p,
  571.          rule->lens[i] - (rule->suffixes[i] - rule->targets[i]) + 1);
  572.       new->file = enter_file (new->name);
  573.       new->next = file->also_make;
  574.       file->also_make = new;
  575.     }
  576.  
  577.  
  578.   return 1;
  579. }
  580.